package com.zt.model.util;

import lombok.extern.log4j.Log4j2;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Objects;
import java.util.Random;

@Log4j2
public class VerifyCodeUtil {
    private static final int WIDTH = 116;//生成的图片的宽度
    private static final int HEIGHT = 36;//生成的图片的高度
    private static String verifyCode = null;
    /**
     * 画验证码图片
     */
    public static BufferedImage drawImg(){
        //1.在内存中创建一张图片
        BufferedImage bi = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_RGB);
        //2.得到图片
        Graphics g = bi.getGraphics();
        g.setColor(new Color(229,224,254));// 设置颜色
        g.fillRect(0, 0, WIDTH, HEIGHT); // 填充区域
        //3.画随机点和线条
        drawRandomPointAndLine(bi,g);
        //4.写在图片上随机数
        verifyCode = drawRandomNum((Graphics2D) g);//生成数字和字母组合的验证码图片
        log.debug("验证码为:"+verifyCode);
        return bi;
    }

    /**
     * 获取验证码字符串
     * @return
     */
    public static String getVerifyCode(){
        if(Objects.isNull(verifyCode)){
           log.debug("没有生成验证码信息");
           return "";
        }
        return verifyCode;
    }

    /**
     * 随机点和随机线条
     * @param bfi
     */
    private static void drawRandomPointAndLine(BufferedImage bfi,Graphics g){
        // 添加噪点
        int area = (int) (0.02 * 80 * 25);
        for(int i=0; i<area; ++i){
            int x = (int)(Math.random() * 80);
            int y = (int)(Math.random() * 25);
            bfi.setRGB(x, y, (int)(Math.random() * 255));
        }

        //在图片上画随机线条
        for (int i = 0; i < 10; i++) {// 设置线条个数并画线
            g.setColor(getRandColor(1, 255));// 设置颜色
            int x1 = new Random().nextInt(WIDTH);
            int y1 = new Random().nextInt(HEIGHT);
            int x2 = new Random().nextInt(WIDTH);
            int y2 = new Random().nextInt(HEIGHT);
            g.drawLine(x1, y1, x2, y2);
        }
    }

    // 获取随机颜色
    private static Color getRandColor(int low, int height) {
        if (low > 255) low = 255;
        if (height > 255) height = 255;
        if (low < 0) low = 0;
        if (height < 0) height = 0;
        int interval = height - low;
        int r = low + (int) (Math.random()*interval);
        int g = low + (int) (Math.random()*interval);
        int b = low + (int) (Math.random()*interval);
        return new Color(r, g, b);
    }
    /**
     * 画随机字符
     * @param g
     * @return
     */
    private static String drawRandomNum(Graphics2D g) {
        // 设置字体
        g.setFont(new Font("微软雅黑", Font.BOLD, 22));
        //数字和字母的组合
        String baseNumLetter = "0123456789ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghjklmnopqrstuvwxyz";

        StringBuffer sb = new StringBuffer();
        int x = 5;
        String ch ="";
        // 控制字数
        for (int i = 0; i < 4; i++) {
            // 设置字体旋转角度
            int degree = new Random().nextInt() % 30;
            ch = baseNumLetter.charAt(new Random().nextInt(baseNumLetter.length())) + "";
            sb.append(ch);
            // 正向角度
            g.rotate(degree * Math.PI / 180, x, 20);
            g.drawString(ch, x, 20);
            g.setColor(getRandColor(0,200));
            // 反向角度
            g.rotate(-degree * Math.PI / 180, x, 20);
            x += 30;
        }
        return sb.toString();
    }

}
